home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / CMATH.ZIP / EXP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  4.1 KB  |  188 lines

  1. /*                            exp.c
  2.  *
  3.  *    Exponential function
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, exp();
  10.  *
  11.  * y = exp( x );
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * Returns e (2.71828...) raised to the x power.
  18.  *
  19.  * Range reduction is accomplished by separating the argument
  20.  * into an integer k and fraction f such that
  21.  *
  22.  *     x    k  f
  23.  *    e  = 2  e.
  24.  *
  25.  * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
  26.  * in the basic range [-0.5 ln 2, 0.5 ln 2].
  27.  *
  28.  *
  29.  * ACCURACY:
  30.  *
  31.  *                      Relative error:
  32.  * arithmetic   domain     # trials      peak         rms
  33.  *    DEC       0, MAXLOG   38000       3.0e-17     6.2e-18
  34.  *    IEEE      +- MAXLOG   35000       2.3e-16     5.7e-17
  35.  *
  36.  *
  37.  * Error amplification in the exponential function can be
  38.  * a serious matter.  The error propagation involves
  39.  * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
  40.  * which shows that a 1 lsb error in representing X produces
  41.  * a relative error of X times 1 lsb in the function.
  42.  * While the routine gives an accurate result for arguments
  43.  * that are exactly represented by a double precision
  44.  * computer number, the result contains amplified roundoff
  45.  * error for large arguments not exactly represented.
  46.  *
  47.  *
  48.  * ERROR MESSAGES:
  49.  *
  50.  *   message         condition      value returned
  51.  * exp underflow    x < MINLOG         0.0
  52.  * exp overflow     x > MAXLOG         MAXNUM
  53.  *
  54.  */
  55.  
  56. /*
  57. Cephes Math Library Release 2.1:  January, 1989
  58. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  59. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  60. */
  61.  
  62.  
  63. /*    Exponential function    */
  64.  
  65. #include "mconf.h"
  66. static char fname[] = {"exp"};
  67.  
  68. #ifdef UNK
  69.  
  70. static double P[] = {
  71.  1.26183092834458542160E-4,
  72.  3.02996887658430129200E-2,
  73.  1.00000000000000000000E0
  74. };
  75. static double Q[] = {
  76. 3.00227947279887615146E-6,
  77. 2.52453653553222894311E-3,
  78. 2.27266044198352679519E-1,
  79. 2.00000000000000000005E0
  80. };
  81. static double C1 = 6.9335937500000000000E-1;
  82. static double C2 = 2.1219444005469058277E-4;
  83. #endif
  84.  
  85. #ifdef DEC
  86. static short P[] = {
  87. 0035004,0050004,0016315,0134545,
  88. 0036770,0033415,0105201,0034462,
  89. 0040200,0000000,0000000,0000000
  90. };
  91. static short Q[] = {
  92. 0033511,0075304,0141275,0061006,
  93. 0036045,0071261,0155620,0021143,
  94. 0037550,0134156,0006512,0174363,
  95. 0040400,0000000,0000000,0000000
  96. };
  97. static short sc1[] = {0040061,0100000,0000000,0000000};
  98. #define C1 (*(double *)sc1)
  99. static short sc2[] = {0035136,0100202,0161410,0062503};
  100. #define C2 (*(double *)sc2)
  101. #endif
  102.  
  103. #ifdef IBMPC
  104. static short P[] = {
  105. 0xb72d,0x8399,0x8a00,0x3f20,
  106. 0x2726,0xb150,0x06e1,0x3f9f,
  107. 0x0000,0x0000,0x0000,0x3ff0
  108. };
  109. static short Q[] = {
  110. 0xac41,0x9857,0x2f58,0x3ec9,
  111. 0x044c,0x3b72,0xae56,0x3f64,
  112. 0x5f1e,0xc1a9,0x170d,0x3fcd,
  113. 0x0000,0x0000,0x0000,0x4000
  114. };
  115. static short sc1[] = {0x0000,0x0000,0x3000,0x3fe6};
  116. #define C1 (*(double *)sc1)
  117. static short sc2[] = {0x0ca8,0x5c61,0xd010,0x3f2b};
  118. #define C2 (*(double *)sc2)
  119. #endif
  120.  
  121. #ifdef MIEEE
  122. static short P[] = {
  123. 0x3f20,0x8a00,0x8399,0xb72d,
  124. 0x3f9f,0x06e1,0xb150,0x2726,
  125. 0x3ff0,0x0000,0x0000,0x0000
  126. };
  127. static short Q[] = {
  128. 0x3ec9,0x2f58,0x9857,0xac41,
  129. 0x3f64,0xae56,0x3b72,0x044c,
  130. 0x3fcd,0x170d,0xc1a9,0x5f1e,
  131. 0x4000,0x0000,0x0000,0x0000
  132. };
  133. static short sc1[] = {
  134. 0x3fe6,0x3000,0x0000,0x0000
  135. };
  136. #define C1 (*(double *)sc1)
  137. static short sc2[] = {
  138. 0x3f2b,0xd010,0x5c61,0x0ca8
  139. };
  140. #define C2 (*(double *)sc2)
  141. #endif
  142.  
  143. extern double LOGE2, LOG2E, MAXLOG, MINLOG, MAXNUM;
  144.  
  145. double exp(x)
  146. double x;
  147. {
  148. double px, qx, xx;
  149. int n;
  150. double polevl();
  151. double floor(), ldexp();
  152.  
  153. if( x > MAXLOG)
  154.     {
  155.     mtherr( fname, OVERFLOW );
  156.     return( MAXNUM );
  157.     }
  158.  
  159. if( x < MINLOG )
  160.     {
  161.     mtherr( fname, UNDERFLOW );
  162.     return(0.0);
  163.     }
  164.  
  165. /* Express e**x = e**g 2**n
  166.  *   = e**g e**( n loge(2) )
  167.  *   = e**( g + n loge(2) )
  168.  */
  169. px = x * LOG2E;
  170. qx = floor( px + 0.5 ); /* floor() truncates toward -infinity. */
  171. n = qx;
  172. x -= qx * C1;
  173. x += qx * C2;
  174.  
  175.  
  176. /* rational approximation for exponential
  177.  * of the fractional part:
  178.  * e**x - 1  =  2x P(x**2)/( Q(x**2) - P(x**2) )
  179.  */
  180. xx = x * x;
  181. px = x * polevl( xx, P, 2 );
  182. x =  px/( polevl( xx, Q, 3 ) - px );
  183. x = ldexp( x, 1 );
  184. x =  x + 1.0;
  185. x = ldexp( x, n );
  186. return(x);
  187. }
  188.